package com.ncmmall.mall.controller.business.live;

import com.ncmmall.application.business.live.LiveSessionApplication;
import com.feijin.commons.exceptions.IllegalRequestException;
import com.feijin.commons.webs.Webs;
import com.ncmmall.domain.business.live.LiveSession;
import com.ncmmall.domain.security.user.WebUser;
import com.ncmmall.querychannel.QueryPage;
import com.ncmmall.querychannel.QueryPageRequest;
import com.ncmmall.querychannel.service.QueryChannelService;
import com.google.common.collect.ImmutableMap;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 主播间实时监控管理
 *
 * @author Samuel
 * @date 2020-09-03 11:06
 */
@Controller
@RequestMapping(value = "/live/session")
@Slf4j
public class LiveSessionController {

    @Autowired
    private QueryChannelService queryChannelService;

    @Autowired
    private LiveSessionApplication liveSessionApplication;

    /**
     * 正在直播列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Model model, QueryPageRequest pageRequest, LiveSession params) {

        Map<String, Object> map = new HashMap<>();
        String hql = buildHQL(map, params);

        QueryPage<LiveSession> page = queryChannelService.findPage(hql, pageRequest.setParams(map), LiveSession.class);
        model.addAttribute("page", page);
        model.addAttribute("params", params);

        return "mall/live/session/list";
    }

    private String buildHQL(Map<String, Object> map, LiveSession params) {

        StringBuilder hql = new StringBuilder("select w from LiveSession w where w.endTime = null ");

        WebUser anchor = params.getAnchor();
        if (anchor != null) {
            if (StringUtils.isNotBlank(anchor.getLiveID())) {
                hql.append(" and w.anchor.liveId like :liveId");
                map.put("liveId", "%" + anchor.getLiveID() + "%");
            }
            if (StringUtils.isNotBlank(anchor.getRealName())) {
                hql.append(" and w.anchor.realName like :realName");
                map.put("realName", "%" + anchor.getRealName() + "%");
            }
            if (StringUtils.isNotBlank(anchor.getMobile())) {
                hql.append(" and w.anchor.mobile like :mobile");
                map.put("mobile", "%" + anchor.getMobile() + "%");
            }
        }

        if (StringUtils.isNotBlank(params.getName())) {
            hql.append(" and w.name like :name");
            map.put("name", "%" + params.getName() + "%");
        }

        hql.append(" order by w.beginTime desc");
        return hql.toString();
    }

    /**
     * 详情
     */
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public String detail(Model model, long id) {
//        WebUser webUser = queryChannelService.findOne(WebUser.class, id);
//        List<WebUser> emp = queryChannelService.findAll(QWebUser.webUser.anchor.id.eq(id), WebUser.class);
//        emp.add(webUser);
//        model.addAttribute("webUser", webUser);
//        model.addAttribute("emp", emp);
//        return "mall/live/detail";
        return "";
    }


    /**
     * 直播间下线
     */
    @RequestMapping(value = "/end", method = RequestMethod.POST)
    @ResponseBody
    private Map<String, Object> end(HttpServletRequest request, long id) {
        try {
            if (!Webs.isAjaxRequest(request)) {
                throw new IllegalRequestException("request must be ajax !");
            }
            LiveSession liveSession = queryChannelService.findOne(LiveSession.class, id);
            liveSessionApplication.end(liveSession.getAnchor());
            return ImmutableMap.of("status", true, "message", "下线成功");
        } catch (Exception e) {
            log.error("[直播间下线异常]", e);
            return ImmutableMap.of("status", false, "message", "下线异常");
        }
    }

    /**
     * 直播间精选设置
     */
    @RequestMapping(value = "/choiceness", method = RequestMethod.POST)
    @ResponseBody
    private Map<String, Object> choiceness(HttpServletRequest request, long id) {
        try {
            LiveSession liveSession = queryChannelService.findOne(LiveSession.class, id);
            liveSessionApplication.choiceness(liveSession);
            return ImmutableMap.of("status", true, "message", "设置成功");
        } catch (Exception e) {
            log.error("[直播间精选设置异常]", e);
            return ImmutableMap.of("status", false, "message", "设置异常");
        }
    }

}
